package schoolWork.work06.test2;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class LoginWindow extends JFrame implements ActionListener {

    JFrame jframe;
    Container cont;
    JPanel panel1,panel2,panel3;
    FlowLayout flowLayout1,flowLayout2;
    JLabel lbl_name;
    JLabel lbl_password;
    JTextField txt_name;
    JPasswordField txt_password;
    JButton btn_OK;
    JButton btn_CANCEL;

    public LoginWindow(){
        jframe = new JFrame("the login window");
        cont = jframe.getContentPane();
        cont.setLayout(new GridLayout(3,1));

        flowLayout1 = new FlowLayout();
        flowLayout1.setHgap(40);
        flowLayout2 = new FlowLayout();

        lbl_name = new JLabel("username");
        lbl_password = new JLabel("password");
        txt_name = new JTextField(15);
        txt_password = new JPasswordField(15);

        btn_OK = new JButton("confirm");
        btn_CANCEL = new JButton("cancel");
        btn_OK.addActionListener(this);
        btn_CANCEL.addActionListener(this);

        panel1 = new JPanel(flowLayout1);
        panel1.add(lbl_name);
        panel1.add(txt_name);

        panel2 = new JPanel(flowLayout1);
        panel2.add(lbl_password);
        panel2.add(txt_password);

        panel3 = new JPanel(flowLayout2);
        panel3.add(btn_OK);
        panel3.add(btn_CANCEL);

        cont.add(panel1);
        cont.add(panel2);
        cont.add(panel3);

        //jframe.setSize(300,150);
        jframe.pack();
        jframe.setLocationRelativeTo(null);
        jframe.setVisible(true);
        jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if(e.getSource() == btn_OK){
            if(txt_name.getText().trim().equals("abc")
                    &&txt_password.getText().trim().equals("1234")) {
                JOptionPane.showMessageDialog(jframe, "欢迎您,abc登录",
                        "系统提示", JOptionPane.INFORMATION_MESSAGE);
                jframe.setVisible(false);
                new mainWindow();
            }
            else {
                JOptionPane.showMessageDialog(jframe,
                        "用户名或密码错误", "系统提示", JOptionPane.WARNING_MESSAGE);
                txt_name.setText("");
                txt_password.setText("");
            }
        }

        if(e.getSource() == btn_CANCEL){
            System.exit(0);
        }

    }
}